package com.example.smartcity.ui.act

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.smartcity.R
import com.example.smartcity.app.App
import com.example.smartcity.http.AdvBannerResp
import kotlinx.android.synthetic.main.activity_spalsh.*
import java.util.*

/**
 * @Author: Tailyou(祝文飞)
 * @CreateDate: 2021/10/7 14:54
 * @Description: 引导页
 */
class SplashActivity : AppCompatActivity() {

    private var timer: Timer? = Timer()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_spalsh)
        loadAdvData()
    }

    // 读取开屏广告数据
    private fun loadAdvData() {
        App.fetchData<AdvBannerResp>(
            "/prod-api/api/rotation/list?pageNum=1&pageSize=8&type=1",
            AdvBannerResp::class.java
        ) {
            renderAdvData(it)
        }
    }

    // 渲染开屏广告数据
    private fun renderAdvData(data: AdvBannerResp) {
        App.fetchRCImage(data.rows[0].advImg, ivAdvise, error = R.mipmap.ic_more_service)
        toMainActivity()
    }

    // 跳转至MainActivity
    private fun toMainActivity() {
        timer?.schedule(object : TimerTask() {
            override fun run() {
                startActivity(Intent(this@SplashActivity, MainActivity::class.java))
                finish()
            }
        }, 1000)
    }

    override fun onDestroy() {
        super.onDestroy()
        timer?.cancel()
        timer?.purge()
        timer = null
    }

}